package cac.fenjian.resource.msaccmat.service;

import cac.common.DateUtil;
import cac.fenjian.resource.MsAccMat.dto.MsAccMatDTO;
import cac.fenjian.resource.MsWpTool.dto.MsWpToolBasicDTO;
import cac.fenjian.resource.msaccmat.dao.MsAccMatDao;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.utils.ComUtil;
import com.cac.dme.adapter.utils.PageHelper;
import com.cac.dme.common.exception.service.DaoException;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class MsAccMatService {

    @Autowired
    private MsAccMatDao msAccMatDao;


    /**
     * 分页查询数据
     * @param queryReqBean
     * @param orderBy
     * @return
     */
    public QueryRespBean<MsAccMatDTO> searchMsAccMatByPage(QueryReqBean<MsAccMatDTO> queryReqBean, String orderBy){
        QueryRespBean<MsAccMatDTO> queryRespBean = new QueryRespBean<>();
        try{
            PageHelper.startPage(queryReqBean.getPageParameter());
            MsAccMatDTO searchParams = queryReqBean.getSearchParams();
            Page<MsAccMatDTO> dataList = msAccMatDao.searchMsAccMatByPage(searchParams,orderBy,queryReqBean.getKeyWord());
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e){
            throw  new DaoException(e.getMessage(),e);
        }
    }


    /**
     * 新增数据
     * @param msAccMatDTO
     * @return
     */
    @Transactional
    public String insertMsAccMatDTO(MsAccMatDTO msAccMatDTO){
        try{
            /**
             * 配置时间变量
             */

            //给数据一个随机的ID
            msAccMatDTO.setMsAccMatId(ComUtil.getId());
            //设置数据创建时间
            msAccMatDTO.setCreationDate(DateUtil.nowTime());
            //操作新增数据
            msAccMatDao.insertMsAccMatDTO(msAccMatDTO);
            return msAccMatDTO.toString();
        }catch (Exception e){
            throw new DaoException(e.getMessage(), e);
        }
    }

    /**
     * 根据前端按钮修改接收物料状态
     * @param id
     * @param type acc：接收 reject：拒收
     * @param remark 拒收原因必填项
     * @return
     */
    @Transactional
    public int changeAccType(String id,String type,String remark){
        if(msAccMatDao.searchById(id)==null){
            throw new DaoException("数据不存在");
        }
        return msAccMatDao.changeAccType(id,type,remark);
    }
}
